{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "logging.basicConfig(level=logging.ERROR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0                5.1               3.5                1.4               0.2\n",
       "1                4.9               3.0                1.4               0.2\n",
       "2                4.7               3.2                1.3               0.2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X, y = load_iris(return_X_y=True, as_frame=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "X[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    0\n",
       "2    0\n",
       "Name: target, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:36 I hypernets.m.hyper_model.py 249 - 3 class detected, inferred as a [multiclass classification] task\n",
      "17:11:36 I hypernets.c.meta_learner.py 22 - Initialize Meta Learner: dataset_id:74e7c134740a0f846f4c5e57fa5e6c93\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trial No.</th>\n",
       "      <th>Previous reward</th>\n",
       "      <th>Best trial</th>\n",
       "      <th>Best reward</th>\n",
       "      <th>Total elapsed</th>\n",
       "      <th>Max trials</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.75</td>\n",
       "      <td>3</td>\n",
       "      <td>0.95</td>\n",
       "      <td>3.398462</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   trial No.  Previous reward  Best trial  Best reward  Total elapsed  \\\n",
       "0         10             0.75           3         0.95       3.398462   \n",
       "\n",
       "   Max trials  \n",
       "0          10  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "#### Current Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Trial No.</th>\n",
       "      <th>Reward</th>\n",
       "      <th>Elapsed</th>\n",
       "      <th>Space Vector</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>0.950000</td>\n",
       "      <td>0.247019</td>\n",
       "      <td>[1, 0, 1, 0, 140, 0, 1, 2, 4, 1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>0.950000</td>\n",
       "      <td>0.163490</td>\n",
       "      <td>[1, 0, 0, 0, 155, 1, 2, 3, 3]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.941667</td>\n",
       "      <td>0.380016</td>\n",
       "      <td>[1, 0, 1, 1, 290, 3, 0, 3, 4]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0.933333</td>\n",
       "      <td>0.242262</td>\n",
       "      <td>[0, 3, 1, 3, 0, 0, 2, 0, 2, 3, 0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.925000</td>\n",
       "      <td>0.473053</td>\n",
       "      <td>[0, 3, 1, 1, 0, 1, 0, 0, 2, 2, 1]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Trial No.    Reward   Elapsed                       Space Vector\n",
       "0          3  0.950000  0.247019   [1, 0, 1, 0, 140, 0, 1, 2, 4, 1]\n",
       "1          8  0.950000  0.163490      [1, 0, 0, 0, 155, 1, 2, 3, 3]\n",
       "2          2  0.941667  0.380016      [1, 0, 1, 1, 290, 3, 0, 3, 4]\n",
       "3          1  0.933333  0.242262  [0, 3, 1, 3, 0, 0, 2, 0, 2, 3, 0]\n",
       "4          4  0.925000  0.473053  [0, 3, 1, 1, 0, 1, 0, 0, 2, 2, 1]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/markdown": [
       "#### Best Trial:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table border=\"1\" class=\"dataframe\">\n",
       "<thead>\n",
       "<tr style=\"text-align: right;\">\n",
       "  <th>key</th>\n",
       "  <th>value</th>\n",
       "</tr>\n",
       "</thead>\n",
       "<tbody><tr>\n",
       "  <td>signature</td>\n",
       "  <td>9f7113def163250b34c8546052657ab1</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>vectors</td>\n",
       "  <td>[1, 0, 1, 0, 140, 0, 1, 2, 4, 1]</td>\n",
       "</tr><tr>\n",
       "  <td>0-estimator_options.hp_or</td>\n",
       "  <td>1</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>1-numeric_imputer_0.strategy</td>\n",
       "  <td>mean</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>2-numeric_scaler_optional_0.hp_opt</td>\n",
       "  <td>True</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>3-Module_LightGBMEstimator_1.boosting_type</td>\n",
       "  <td>gbdt</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>4-Module_LightGBMEstimator_1.num_leaves</td>\n",
       "  <td>140</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>5-Module_LightGBMEstimator_1.max_depth</td>\n",
       "  <td>3</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>6-Module_LightGBMEstimator_1.learning_rate</td>\n",
       "  <td>0.01</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>7-Module_LightGBMEstimator_1.reg_alpha</td>\n",
       "  <td>0.1</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>8-Module_LightGBMEstimator_1.reg_lambda</td>\n",
       "  <td>1</td>\n",
       "</tr>\n",
       "<tr><tr>\n",
       "  <td>9-numeric_or_scaler_0.hp_or</td>\n",
       "  <td>1</td>\n",
       "</tr>\n",
       "<tr>  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "DAG_HyperSpace_1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:36 I hypernets.d.in_process_dispatcher.py 119 - Trial 1 done, reward: 0.9333333333333333, best_trial_no:1, best_reward:0.9333333333333333\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:37 I hypernets.d.in_process_dispatcher.py 119 - Trial 2 done, reward: 0.9416666666666667, best_trial_no:2, best_reward:0.9416666666666667\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:37 I hypernets.d.in_process_dispatcher.py 119 - Trial 3 done, reward: 0.95, best_trial_no:3, best_reward:0.95\n",
      "\n",
      "17:11:38 I hypernets.d.in_process_dispatcher.py 119 - Trial 4 done, reward: 0.925, best_trial_no:3, best_reward:0.95\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:38 I hypernets.d.in_process_dispatcher.py 119 - Trial 5 done, reward: 0.0, best_trial_no:3, best_reward:0.95\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:38 I hypernets.d.in_process_dispatcher.py 119 - Trial 6 done, reward: 0.9083333333333333, best_trial_no:3, best_reward:0.95\n",
      "\n",
      "17:11:39 I hypernets.d.in_process_dispatcher.py 119 - Trial 7 done, reward: 0.925, best_trial_no:3, best_reward:0.95\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "categorical_feature in Dataset is overridden.\n",
      "New categorical_feature is []\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17:11:39 I hypernets.d.in_process_dispatcher.py 119 - Trial 8 done, reward: 0.95, best_trial_no:3, best_reward:0.95\n",
      "\n",
      "17:11:39 I hypernets.d.in_process_dispatcher.py 119 - Trial 9 done, reward: 0.75, best_trial_no:3, best_reward:0.95\n",
      "\n",
      "17:11:40 I hypernets.d.in_process_dispatcher.py 119 - Trial 10 done, reward: 0.925, best_trial_no:3, best_reward:0.95\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from hypergbm.search_space import search_space_general\n",
    "from hypergbm import HyperGBM\n",
    "from hypernets.searchers import MCTSSearcher\n",
    "\n",
    "rs = MCTSSearcher(search_space_general, max_node_space=10, optimize_direction='max')\n",
    "hk = HyperGBM(rs, task='multiclass', reward_metric='accuracy', callbacks=[])\n",
    "hk.search(X_train, y_train, X_eval=None, y_eval=None, cv=3)  # using Cross Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[LGBMClassifierWrapper(learning_rate=0.01, max_depth=3, n_estimators=200,\n",
       "                       num_leaves=140, reg_alpha=0.1, reg_lambda=1),\n",
       " LGBMClassifierWrapper(learning_rate=0.01, max_depth=3, n_estimators=200,\n",
       "                       num_leaves=140, reg_alpha=0.1, reg_lambda=1),\n",
       " LGBMClassifierWrapper(learning_rate=0.01, max_depth=3, n_estimators=200,\n",
       "                       num_leaves=140, reg_alpha=0.1, reg_lambda=1)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "estimator = hk.load_estimator(hk.get_best_trial().model_file)\n",
    "estimator.cv_gbm_models_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
